﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using UnityEngine;

namespace Assets.Scripts
{
    class TaskJobOne : TaskBase<TaskJobOne>
    {
        private Stopwatch stopwatch = new Stopwatch();

        public override string TaskName
        {
            get { return "TaskJobOne"; }
        }

        private int taskId;
        public override int TaskID
        {
            get{  return taskId; }
            set{ taskId = value; }
        }

        private TaskStatus _taskStatus;
        public override TaskStatus taskStatus
        {
            get { return _taskStatus; }
            set
            {
                _taskStatus = value;
                OnStatueChanged(_taskStatus);
            }
        }

        private TimeSpan eslapsed = new TimeSpan();
        public override TimeSpan Eslapsed
        {
            get { return eslapsed; }
            set { eslapsed = value; }
        }

        private bool isCanReverse;
        public override bool IsCanReverse
        {
            get { return IsCanReverse; }
        }

        public override void OnReverse()
        {
            base.OnReverse();
        }

        public override void OnFinal()
        {
            stopwatch.Stop();
            eslapsed = stopwatch.Elapsed;
        }

        public override void OnDoTask(CancellationToken cancellationToken)
        {
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    taskStatus = TaskStatus.Cancle;
                    return;
                }

                taskStatus = TaskStatus.Start;
                string path = base.FilePath;
                using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
                {
                    StreamWriter sw = new StreamWriter(fs);
                    for (int i = 0; i < 100; i++)
                    {
                        sw.WriteLine(i + " This is a test file!");
                        taskStatus = TaskStatus.Running;
                    }
                    sw.Flush();
                    sw.Flush();
                }
                taskStatus = TaskStatus.Success;

            }
            catch (Exception ex)
            {
                taskStatus = TaskStatus.Fail;
                throw ex;
            }
        }

        public override void OnReady()
        {
            stopwatch.Restart();
        }

        public override void OnStatueChanged(TaskStatus taskStatus)
        {
            switch (taskStatus)
            {
                case TaskStatus.None:
                    break;
                case TaskStatus.Start:
                    OnReady();
                    UnityEngine.Debug.LogFormat("<color=yellow>Task Start,taskId:{0}</color>", taskId);
                    break;
                case TaskStatus.Running:
                    break;
                case TaskStatus.Success:
                case TaskStatus.Fail:
                    OnFinal();
                    UnityEngine.Debug.LogFormat("<color=yellow>Task Finish,taskId:{0}</color>", taskId);
                    UnityEngine.Debug.LogFormat("<color=yellow>Task Cost Time:{0}</color>", eslapsed.TotalSeconds+"s");
                    UnityEngine.Debug.Log("--------------------------------------------------------");
                    break;
                case TaskStatus.Cancle:
                    UnityEngine.Debug.LogFormat("<color=yellow>Task canclled,taskId:{0}</color>", taskId);
                    break;
                default:
                    break;
            }
        }
    }
}
